package L7.L713;

/**
 *岛屿最大面积
 * @author: luolidong
 * @create: 2021-07-13 20:33
 * @version: 1.0
 */
public class MaxIsland {
    public int maxAreaOfIsland(int[][] grid) {
        if(grid==null||grid.length==0) return 0;
        int max=0;
        for (int i = 0; i <grid.length ; i++) {
            for (int j = 0; j <grid[0].length ; j++) {
                if(grid[i][j]==1){
                    max=Math.max(max,dfs(grid,i,j,0));
                }
            }
        }
        return max;
    }
    public int dfs(int[][] grid,int i,int j,int count){
        if(i<0||i>=grid.length||j<0||j>=grid[0].length||grid[i][j]==0){
            return 0;
        }
        count++;
        grid[i][j]=2;
        dfs(grid, i-1, j, count);
        dfs(grid, i+1, j, count);
        dfs(grid, i, j-1, count);
        dfs(grid, i, j+1, count);
        return count;
    }
}
